
/*************************************************************
  #include <User_Setup.h>
  ------------------------------------------------------------
  #define GC9A01_DRIVER   #define SPI_FREQUENCY  40000000
  #define TFT_WIDTH  240 / TFT_HEIGHT 240 // GC9A01 240 x 240
  ------------------------------------------------------------
  #define TFT_MOSI 15  //SDA/DIN  #define TFT_SCLK 14  //CLK
  #define TFT_CS   5   //CS       #define TFT_DC   27  //DC
  #define TFT_RST  33  //RST      #define TFT_BL   22  //BL
***************************************************************/

#include "esp32_display.h"
#include <TFT_eSPI.h>

TFT_eSPI tft = TFT_eSPI();

static const uint32_t screenWidth = 240;
static const uint32_t screenHeight = 240;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * 10];

void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors((uint16_t *)&color_p->full, w * h, true);
    tft.endWrite();
    lv_disp_flush_ready(disp);
}

void ESP32Display::init()
{
    lv_init();
    tft.begin();
    tft.setRotation(0);
    lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * 10);
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register(&disp_drv);
}

void ESP32Display::routine()
{
    lv_timer_handler();
}
